/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../ExtenderDuplicator.debug.js" />
/// <reference path="../../Resources/js/jquery/jquery.debug.js" />


Type.registerNamespace("BM");

// DynamicPopulateAddition object

BM.DynamicPopulateAddition = function(item, additionNode) {
    BM.DynamicPopulateAddition.initializeBase(this, [item, additionNode]);
    this.item.addChanged(Function.createDelegate(this, this.raiseItemChanged));
    var extender = this.createOrGetExtender();
    extender.add_populated(Function.createDelegate(this, this.raisePopulated));
}

BM.DynamicPopulateAddition.prototype = {
    raiseItemChanged: function() {
        this.getExtender().populate(this.item.getValue());
    },

    raisePopulated: function(event, args) {
        $(event.get_element()).trigger('change');
    },

    createOrGetExtender: function() {
        var extender = this.getExtender();
        if (extender == null) {
            extender = this.createExtender();
        }
        return extender;
    },

    getExtender: function() {
        return $find(this.item.getId() + "_DP");
    },

    getItemId: function() {
        return this.item.getId();
    },

    getPrototypeId: function() {
        return this.item.basis.node.id;
    },

    createExtender: function() {
        BM.ExtenderDuplicator.run(this.getPrototypeId(), this.getItemId());
        return this.getExtender();
    }

}

BM.DynamicPopulateAddition.registerClass("BM.DynamicPopulateAddition", BM.Addition);
BM.Addition['DynamicPopulateAddition'] = BM.DynamicPopulateAddition;

